﻿using AutoMapper;
using AutoMapper.QueryableExtensions;

using Auto.IRepository.IRepositories;
using Auto.Model;
using Auto.Model.Dto;
using Auto.Model.Entities;

using Microsoft.EntityFrameworkCore.Internal;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using System.Threading;
using Auto.Tools.Standards.ServiceResult;

namespace Auto.Repository.Repositories
{
    public class CorporationRepository : RepositoryBase<Corporation>, ICorporationRepository
    {
        public CorporationRepository(EFDbContext context, IMapper mapper) : base(context)
        {
            Mapper = mapper;
        }

        public IMapper Mapper { get; }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="whereLambda">Lambda查询条件</param>
        /// <returns></returns>
        public bool Delete(Expression<Func<Corporation, bool>> whereLambda, long DeleterUserID)
        {
            Corporation entity = dbContext.Set<Corporation>().Where(whereLambda).FirstOrDefault();
            if (entity is null)
            {
                return false;
            }
            entity.IsActive = false;
            entity.IsDeleted = true;
            entity.DeleterUserID = DeleterUserID;
            entity.DeletionTime = DateTime.Now.ToLocalTime();
            Delete(entity);
            return true;
        }
        /// <summary>
        /// 实体是否有效
        /// </summary>
        /// <param name="corporation"></param>
        /// <returns></returns>
        public bool IsUseful(Corporation corporation)
        {
            if (corporation == null || corporation.IsActive != true || corporation.IsDeleted != false)
            {
                return false;
            }
            return true;
        }

        public ServicePageDataResult<CorporationDto> LoadPageDataList(Expression<Func<CorporationDto, bool>> whereLambda,
                                                         Expression<Func<CorporationDto, object>> orderLambda,
                                                         int pageIndex,
                                                         int pageSize,
                                                         bool isDesc = false)
        {
            if (whereLambda == null)
            {
                whereLambda = w => true;
            }
            if (orderLambda != null)
            {
                orderLambda = c => c.ID;
            }

            //  var s = dbContext.Corporations.GroupJoin()
            var dataSource =
             dbContext
             .Corporations
             .GroupJoin(dbContext.Areas, c => c.AreaID, a => a.ID, (c, a) => new
             {
                 corporation = c,
                 area = a
             })
             .SelectMany(combine => combine.area.DefaultIfEmpty(), (combines, area) => new CorporationDto
             {
                 AreaAddress = area.MergerName,
                 AreaID = area.ID,
                 AreaLevel = area.LevelType,
                 ID = combines.corporation.ID,
                 Name = combines.corporation.Name,
                 FullName = combines.corporation.FullName,
                 BusinessLicense = combines.corporation.BusinessLicense,
                 CorporationAddress = combines.corporation.CorporationAddress,
                 CreationTime = combines.corporation.CreationTime,
                 CreatorUserID = combines.corporation.CreatorUserID,
                 Departments = combines.corporation.Departments,
                 IsDeleted = combines.corporation.IsDeleted,
                 IsActive = combines.corporation.IsActive,
                 LegalPersonIDCardNo = combines.corporation.LegalPersonIDCardNo,
                 LastModificationTime = combines.corporation.LastModificationTime,
                 LegalPerson = combines.corporation.LegalPerson,
                 LegalPersonPhone = combines.corporation.LegalPersonPhone,
                 FaxNumber = combines.corporation.FaxNumber,
                 Remarks = combines.corporation.Remarks,
                 SupportEMail = combines.corporation.SupportEMail,
                 Telephone = combines.corporation.Telephone
             })
             .Where(whereLambda);
            ServicePageDataResult<CorporationDto> resualData = new ServicePageDataResult<CorporationDto>();
            if (dataSource == null || dataSource.Count() < 1)
            {
                return resualData;
            }
            if (isDesc)
            {
                dataSource = dataSource.OrderByDescending(orderLambda);
            }
            resualData.Total = dataSource.Count();
            dataSource = dataSource.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            resualData.Data = dataSource.ToList();
            return resualData;
        }


    }
}
